package org.projectname.web.admin.manager;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.hishenframework.core.data.jdbc.template.DataPaging;
import org.hishenframework.core.web.menu.MenuItem;
import org.projectname.entity.manager.Role;
import org.projectname.service.admin.manager.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(RoleController.PATH)
@MenuItem(type = "账号管理", label = "角色管理", order = 98)
public class RoleController {
	
	public final static String PATH = "/admin/manager/role";
	public final static String RELOAD = "redirect:" + PATH;
	
	@Resource
	private RoleService roleService;
	
	@RequestMapping
	public String list(@ModelAttribute DataPaging<Role> dataPaging, String role){
		roleService.search(dataPaging, role);
		return PATH + "/list";
	}
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable int id, Model model){
		model.addAttribute("role", roleService.get(id));
		return PATH + "/edit";
	}
	
	@RequestMapping("/create")
	public String create(){
		return PATH + "/create";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@Valid Role entity, BindingResult bindingResult){
		if(!bindingResult.hasErrors()){
			roleService.save(entity);
		}
		return RELOAD;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@Valid Role entity, BindingResult bindingResult){
		if(!bindingResult.hasErrors()){
			roleService.update(entity);
		}
		return RELOAD;
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable int id){
		roleService.delete(id);
		return RELOAD;
	}
}
